<?php

/**
 * @copyright       : FOSP
 * @version         : 20120323
 * @todo            : Fixture controller
 * @name            : FixtureController
 * @author          : QuangTM
 */
class Euro2012_FixtureController extends Zend_Controller_Action
{
     /**
     * init of controller
     */
    public function init() {
        
    }
    public function dauloaiAction(){
        $teamMathModel = Match::getInstance();
         $teamSeasonModel = TeamSeason::getInstance();
        $arrTeam = $this->view->arrTableMatch;  
        $arrMatchIDs = $teamMathModel->getMatchIDsBySeasonAndLeague(57,8);
        $arrMatchDetails = $teamMathModel->getMatchDetailByIDs ($arrMatchIDs);
        // echo "<pre>";print_r($arrMatchDetails);die();
        $count = count(array_keys($arrTeam));
        $arrTeam['quarter-finals']['Matches']  =   $arrTeam['semi-finals']['Matches']  = $arrTeam['final']['Matches'] = array();
        foreach ($arrMatchDetails as $matchID => $matchDetail) 
        {
            switch ($matchDetail['Round']) {
                case 'Quarter-finals':
                    /*if (!isset ($arrTeam['group-a']['Matches']))
                    {
                        $arrTeam['group-a']['Matches'] = array();
                    }*/
                    $arrTeam['quarter-finals']['Matches'][]    = $matchDetail;
                    //array_push($arrTeam['group-a']['Matches'], $matchDetail);
                    break;
                case 'Semi-finals':
                    /*if (!isset ($arrTeam['group-b']['Matches']))
                    {
                        $result['group-b']['Matches'] = array();
                    }*/
                    $arrTeam['semi-finals']['Matches'][]    = $matchDetail;
                    //array_push($arrTeam['group-b']['Matches'], $matchDetail);
                    break;
                case 'Final':
                    /*if (!isset ($arrTeam['group-c']['Matches']))
                    {
                        $result['group-c']['Matches'] = array();
                    }*/
                    $arrTeam['final']['Matches'][]    = $matchDetail;
                    //array_push($arrTeam['group-c']['Matches'], $matchDetail);
                    break;
               
                default:
                    break;
            }
            // virtual data
           // $i++;
        }
         $this->view->arrTeam = $arrTeam;
        //echo "<pre>";print_r($arrTeam);die();
    }
    public function indexAction ()
    {     
        $teamSeasonModel = TeamSeason::getInstance();
        $arrTeam = $this->view->arrTableMatch;        
        
        //$this->view->arrTeam = $arrTeam;
       
        $teamMathModel = Match::getInstance();
         
        $arrMatchIDs = $teamMathModel->getMatchIDsBySeasonAndLeague(57,8);
        $arrMatchDetails = $teamMathModel->getMatchDetailByIDs ($arrMatchIDs);

        $count = count(array_keys($arrTeam));
        $arrTeam['group-a']['Matches']  =   $arrTeam['group-b']['Matches']  = $arrTeam['group-c']['Matches']    = $arrTeam['group-d']['Matches']    = array();
        foreach ($arrMatchDetails as $matchID => $matchDetail) 
        {
            switch ($matchDetail['Round']) {
                case 'Group A':
                    /*if (!isset ($arrTeam['group-a']['Matches']))
                    {
                        $arrTeam['group-a']['Matches'] = array();
                    }*/
                    $arrTeam['group-a']['Matches'][]    = $matchDetail;
                    //array_push($arrTeam['group-a']['Matches'], $matchDetail);
                    break;
                case 'Group B':
                    /*if (!isset ($arrTeam['group-b']['Matches']))
                    {
                        $result['group-b']['Matches'] = array();
                    }*/
                    $arrTeam['group-b']['Matches'][]    = $matchDetail;
                    //array_push($arrTeam['group-b']['Matches'], $matchDetail);
                    break;
                case 'Group C':
                    /*if (!isset ($arrTeam['group-c']['Matches']))
                    {
                        $result['group-c']['Matches'] = array();
                    }*/
                    $arrTeam['group-c']['Matches'][]    = $matchDetail;
                    //array_push($arrTeam['group-c']['Matches'], $matchDetail);
                    break;
                case 'Group D':
                    /*if (!isset ($arrTeam['group-d']['Matches']))
                    {
                        $result['group-d']['Matches'] = array();
                    }*/
                    $arrTeam['group-d']['Matches'][]    = $matchDetail;
                    //array_push($arrTeam['group-d']['Matches'], $matchDetail);
                    break;
                default:
                    break;
            }
            // virtual data
           // $i++;
        }
//        echo "<pre>";
//        print_r($this->view->arrTeam);exit;
        $this->view->arrTeam = $arrTeam;
       
    }
    
    public function chitietAction ()
    { 
        // Disable render layout
        $this->_helper->layout->disableLayout (true);
        $this->_helper->viewRenderer->setNoRender (true);

        $response = array (
            'error' => 0,
            'msg'   => 'Success',
            'html'  => NULL,
        );

        $date     = $this->_request->getParam ('ngay', NULL);
        $leagueId = intval ($this->_request->getParam ('lid', 0));
        $group    = $this->_request->getParam ('g', NULL);

        // Determine group
        if ($group != 'quoc-te' && $group != 'trong-nuoc')
            $group = NULL;

        if (!$leagueId)
            $leagueId = NULL;

        // Determine date to get fixture
        if (empty ($date))
            $unixTime = strtotime (date ('Y/m/d'));
        else
            $unixTime = strtotime ($date);

        // Get models' instance
        $matchModel  = Match::getInstance ();
        $leagueModel = League::getInstance ();

        // Get list league 
        // Quoc te
        $arrListLeagueQT = $arrListLeagueTN = NULL;
        $groups          = array (1001, 2001);
        if ($group == NULL || $group == 'quoc-te')
            $arrListLeagueQT = $leagueModel->getListLeagueByGroup (1001);
        if ($group == NULL || $group == 'trong-nuoc')
            $arrListLeagueTN = $leagueModel->getListLeagueByGroup (2001);

        // Get list matches' id by happen time
        $arrMatchIDs = $matchModel->getMatchIDsHappenTime ($unixTime, $leagueId);

        // Get list matches' detail
        $arrMatchDetails = $matchModel->getMatchDetailByIDs ($arrMatchIDs);

        $fixture = array ();
        foreach ($arrMatchDetails as $match)
        {
            if (!isset ($fixture[$match['Group']]))
            {
                $fixture[$match['Group']] = array (
                    $match['LeagueID'] => array (
                        'Name'       => $match['LeagueName'],
                        'SeasonName' => $match['SeasonName'],
                        'LeagueID'   => $match['LeagueID'],
                        'SeasonID'   => $match['SeasonID'],
                        'arrMatches' => array ($match)
                    )
                );
            }
            else
            {
                if (isset ($fixture[$match['Group']][$match['LeagueID']]))
                {
                    array_push ($fixture[$match['Group']][$match['LeagueID']]['arrMatches'], $match);
                }
                else
                {
                    $fixture[$match['Group']][$match['LeagueID']] = array (
                        'Name'       => $match['LeagueName'],
                        'SeasonName' => $match['SeasonName'],
                        'LeagueID'   => $match['LeagueID'],
                        'SeasonID'   => $match['SeasonID'],
                        'arrMatches' => array ($match)
                    );
                }
            }
        }

        // Assign to view
        $this->view->assign (array (
            'group'           => $group,
            'currTime'        => time (),
            'leagueId'        => intval ($leagueId),
            'prevUnixTime'    => $unixTime - 86400,
            'currUnixTime'    => $unixTime,
            'nextUnixTime'    => $unixTime + 86400,
            'fixture'         => $fixture,
            'groups'          => $groups,
            'arrListLeagueQT' => $arrListLeagueQT,
            'arrListLeagueTN' => $arrListLeagueTN,
        ));
        
        $response['html'] = $this->view->render('fixture/chitiet.phtml');
        echo Zend_Json::encode($response);
        exit;
    }

   
    public function doibongAction()
    {
        $leagueId = intval($this->_request->getParam('lid', 1));
        $seasonId = intval($this->_request->getParam('sid', 0));
        
        $teamSeasonModel = TeamSeason::getInstance();
        
        $tableRanking = $teamSeasonModel->getListTableRanking($seasonId, $leagueId);
        $result = array();
        foreach ($tableRanking as $ranking) 
        {
            if (!isset ($result[$ranking['Table']]))
            {
                $result[$ranking['Table']] = array(
                    'Ranking' => array($ranking),
                );
            }
            else
            {
                array_push ($result[$ranking['Table']]['Ranking'], $ranking);
            }
        }
       
        $unixTime = strtotime (date ('Y/m/d')) - 2 * 3600 * 24;
        $matchModel  = Match::getInstance();
        
        $arrMatchIDs = $matchModel->getMatchIDsHappenTime ($unixTime, 1);
        // Virtual data => remove  if have true data
        $arrMatchIDs = array(1,2,3,4,5,6,7,8);

        // Get list matches' detail
        $arrMatchDetails = $matchModel->getMatchDetailByIDs ($arrMatchIDs);
        
        // Virtual data
        $i = 0;
        
        // Count number table
        $count = count(array_keys($result));
        
        foreach ($arrMatchDetails as $matchID => $matchDetail) 
        {
            // Virtual data
            switch ($i % $count) {
                case 0:
                    $matchDetail['Round'] = 'A';
                    break;
                case 1:
                    $matchDetail['Round'] = 'B';
                    break;
                case 2:
                    $matchDetail['Round'] = 'C';
                    break;
                case 3:
                    $matchDetail['Round'] = 'D';
                    break;
                default:
                    break;
            }
            
            switch ($matchDetail['Round']) {
                case 'A':
                    if (!isset ($result['group-a']['Matches']))
                    {
                        $result['group-a']['Matches'] = array();
                    }
                    array_push($result['group-a']['Matches'], $matchDetail);
                    break;
                case 'B':
                    if (!isset ($result['group-b']['Matches']))
                    {
                        $result['group-b']['Matches'] = array();
                    }
                    array_push($result['group-b']['Matches'], $matchDetail);
                    break;
                case 'C':
                    if (!isset ($result['group-c']['Matches']))
                    {
                        $result['group-c']['Matches'] = array();
                    }
                    array_push($result['group-c']['Matches'], $matchDetail);
                    break;
                case 'D':
                    if (!isset ($result['group-d']['Matches']))
                    {
                        $result['group-d']['Matches'] = array();
                    }
                    array_push($result['group-d']['Matches'], $matchDetail);
                    break;
                default:
                    break;
            }
            // virtual data
            $i++;
        }
        
        
//        echo '<pre>';
//        print_r($result);exit;
        $this->view->assign('fixture',$result);
        
       
    }
    
    public function dauloai1Action(){
        //exit;
         $teamSeasonModel = TeamSeason::getInstance();
        $arrMatches = $teamSeasonModel->getListMath( 57, 8);
       
        
        $result = array();
        foreach ($arrMatches as $match) 
        {
           // switch ($match['group']) {
               // case 'Group A':
                    if (!isset ($result[$match['Round']]))
                    {
                        $result[$match['Round']] = array($match);
                    }
                    else
                    {
                        array_push ($result[$match['Round']], $match);
                    }
               // break;
            //}
        }
        // Lich thi dau
        $arrMatches = $result;  
        $this->view->arrMatches = $arrMatches;
    }
     //cap dau
//         $arrTeam1 = $this->view->arrTableMatch1;        
//        
//        $this->view->arrTeam1 = $arrTeam1;
//        
//        echo '<pre>';print_r( $this->view->arrTeam1);die
        
        
//        $arrMatches = $teamSeasonModel->getListMath( 57, 8);
//       
//        
//        $result = array();
//        foreach ($arrMatches as $match) 
//        {
//           // switch ($match['group']) {
//               // case 'Group A':
//                    if (!isset ($result[$match['round']]))
//                    {
//                        $result[$match['round']] = array($match);
//                    }
//                    else
//                    {
//                        array_push ($result[$match['round']], $match);
//                    }
//               // break;
//            //}
//        }
//        // Lich thi dau
//        $arrMatches = $result;  
//        $this->view->arrMatches = $arrMatches;
//        var_dump('<pre>',$arrMatches);exit;
}